<?php

// put full path to Smarty.class.php

$smartyPath = "/home/geeka813/Smarty-3.1.13/libs";
$publicHtml = "/home/geeka813/public_html";
$base_css = "/css";
$base_images = "/images";

// contains all smarty declarations
require_once($publicHtml . '/config.php');

$log->LogInfo("Processing scratch page");

$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$menuSql = "SELECT data FROM page_item WHERE pageid=5";
  			
$result = $pdo->query($menuSql);
//# Map results to object
//$result->setFetchMode(PDO::FETCH_CLASS, 'pageid');
$results = $result->fetchAll(PDO::FETCH_ASSOC);   		
		
$carousel1 = array();
$menu1 = array();
$slider1 = array();
$games = array();

$admin = false;


$approved = 0;

if(isset($_REQUEST['approved']))
{
   if(isset($_REQUEST['gid']))
   {
      if(isset($_REQUEST['adminid']) && $_REQUEST['adminid'] == 'geeka_alian1')
      {
         $gameSql = "SELECT game_info FROM scratch_game WHERE id={$_REQUEST['gid']} AND status='approved'";
         
         $gameResult = $pdo->query($gameSql);
         $gameResults = $gameResult->fetchAll(PDO::FETCH_ASSOC);
        
         if(sizeof($gameResults) == 0)
         {
           $log->LogInfo("No results found for '" . $gameSql . "'");
           header("Location: scratch.php"); /* Redirect browser */
           exit;
         }
         
         $approved = 1;
         
         $gameinfo = $gameResults[0]['game_info'];
	 $gameInfoObj = json_decode($gameinfo);
         
         $obj = new stdClass();
         $obj->type = "gameupload";
         $obj->name = $gameInfoObj->name;
         $obj->email = $gameInfoObj->email;
         $obj->gameurl = "www.geekawhat.com/scratchgame.php?gid=" . $_REQUEST['gid'];
         
         sendEmail($obj);
         
         // TODO send email to confirm game is on site
      }
      
    }
}

$smarty->assign("approved", $approved);

$log->LogInfo("Approved = " . $approved);


if(isset($_REQUEST['gid']))
{
	$gameID = $_REQUEST['gid'];
	
	$filename = "{$publicHtml}/Scratch/projects/{$gameID}/{$gameID}.sb";
	
	$gameSql = "SELECT status,game_info FROM scratch_game WHERE id={$gameID}";
	
	$preview = false;
	
	if(isset($_REQUEST['preview']))
        {
	   if(isset($_REQUEST['adminid']) && $_REQUEST['adminid']='geeka_alian1')
	   {
	       $admin = true;
	       $preview = true;
	   }
        }
        
        if($preview == false)
        {
           $gameSql .= " AND status='approved'";
        }
        
        $gameResult = $pdo->query($gameSql);
        $gameResults = $gameResult->fetchAll(PDO::FETCH_ASSOC);
        
        if(sizeof($gameResults) == 0)
        {
           $log->LogInfo("No results found for '" . $gameSql . "'");
           header("Location: scratch.php"); /* Redirect browser */
           exit;
        }  
	
	if(file_exists($filename))
	{
		$smarty->assign("gameid", $gameID);
		
		$decoded = json_decode($gameResults[0]['game_info']);
		
		$smarty->assign("title", $decoded->gametitle);
		$smarty->assign("gamedescription", $decoded->gamedescription);
		$smarty->assign("instructions", $decoded->instructions);
		$smarty->assign("name", $decoded->name);
		
		if($approved == 1)
		{
		   $log->LogInfo("Assigning email to " . $decoded->email);
		   $log->LogInfo("decoded = " . print_r($decoded, TRUE));
		   $smarty->assign("email", $decoded->email);
		}
	}
	else
	{
	        $log->LogInfo("File does not exist for gameID " . $gameID);
		header("Location: scratch.php"); /* Redirect browser */
		exit;
	}
}




if(sizeof($results) > 0)
{
	foreach($results as $row)
	{
		if($row['data'] != "")
		{
			$dataObj = json_decode($row['data']);
			$log->LogInfo("Processing item dataObj = " . print_r($dataObj, TRUE));
			
			switch($dataObj->type)
			{	
				case "menu1":
				{
					$obj = new stdClass();
					
					$obj->name = $dataObj->name;
					$obj->submenus = $dataObj->submenus;
					
					$menu1[] = $obj;
				}
				break;
				
				case "slider1":
				{
					$obj = new stdClass();
					$obj->url = $dataObj->url;
					
					$slider1[] = $obj;
				}
				break;
				
			}
		}
	}
}

function sendEmail($messageDetails)
{
   global $log, $smarty;
   $log->LogInfo("In sendEmail, messageDetails = " . print_r($messageDetails, TRUE));
   
   
   $to = $messageDetails->email;
   $log->LogInfo("here1");
   $subject = "Scratch - Your Game is approved! | GeekaWhat";
   $log->LogInfo("here2");

   $from = "scratchprojects@geekawhat.com";
   $log->LogInfo("here4");

   $headers = "From:" . $from . "\r\n";
   $headers .= "Reply-To:" . $from . "\r\n";
   $log->LogInfo("here6");


   $headers .= "MIME-Version: 1.0\r\n";
      $log->LogInfo("here7");

   $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
      $log->LogInfo("here8");

   
   $log->LogInfo("about to fetch email html for message to {$to}");
   
   $smarty->assign("name", $messageDetails->name);
   $smarty->assign("base_css", "www.geekawhat.com/css");
   $smarty->assign("base_images", "www.geekawhat.com/images");
   $smarty->assign("gameurl", $messageDetails->gameurl);
   $log->LogInfo("here9");

   $message = $smarty->fetch("game_approved.tpl");
   
   $log->LogInfo("Sending email message is '{$message}'");
   $log->LogInfo("Sending email to " . $to);

   mail($to,$subject,$message,$headers);

}


$log->LogInfo("carousel1 = " . print_r($carousel1, TRUE));
$log->LogInfo("menu1 = " . print_r($menu1, TRUE));

$smarty->assign("carousel1", $carousel1);
$smarty->assign("games", $games);
$smarty->assign("menu1", $menu1);
$smarty->assign("slider1", $slider1);
$smarty->assign("base_css", $base_css);
$smarty->assign("base_images", $base_images);

$crumbTrail = array();
$crumbTrail["Home > "] = "index.php";
$crumbTrail["Scratch > "] = "scratch.php";
$crumbTrail[$decoded->gametitle] = "";

$smarty->assign("crumbtrail", $crumbTrail);


$smarty->display('scratchgame.tpl');

?>